<template>
	<view>
		<view class="content">
			<view class="row b-b">
				<text class="tit">联系人</text>
				<input class="input" type="text" placeholder="收货人姓名" placeholder-class="placeholder" v-model="userName"/>
			</view>
			<view class="row b-b">
				<text class="tit">手机号</text>
				<input class="input" type="number" placeholder="收货人手机号码" placeholder-class="placeholder" v-model="userPhone"/>
			</view>
			<view class="row b-b">
				<text class="tit">选择地址</text>
				<view @click="show = true">{{city}}</view>
			</view>
		</view>
		<view class="row b-b">
			<text class="tit">详细地址</text>
			<input class="input" type="text" placeholder="请输入详细地址" placeholder-class="placeholder" v-model="detail"/>
		</view>
		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch color="#06923D" :checked="defaultType" @change="switch1Change"/>
		</view>
		<button class="add-btn" @click="addRess">提交</button>
		<cityselect  v-model="show" @city-change="cityChange" @isshow="show=false"></cityselect>
	</view>
	
</template>

<script>
	import {addAddress} from "../../api/product.js"
	import cityselect from"../../components/cityselect.vue"
	export default {
			data() {
				return {
					show: false,
					city:"选择地址",
					defaultType:false,//默认地址
					userName:"",
					userPhone:"",
					area_id:'',  //地区id
					detail:"",//详细地址
					addressId:0//地址id,
				}
			},
			components:{
				cityselect
			},
			onLoad(e){
				if(e.obj){
					console.log(JSON.parse(e.obj))
					let data=JSON.parse(e.obj)
					this.userName=data.name
					this.userPhone=data.phone
					this.area_id=data.area_id
					this.detail=data.detail
					this.addressId=data.id
					this.city =data.province + '-' + data.city + '-' +data.area
					if(data.is_default==1){
						this.defaultType=true
					}else{
						this.defaultType=false
					}
				}
			},
			methods:{
				cityChange(e) {
					console.log(e)
					console.log(e)
					this.city=e.area.merger_name
					this.area_id=e.area.id
					// console.log(this.city)
				},
				// 选择默认
				switch1Change(e){
					console.log(e)
					this.defaultType=e.detail.value
				},
				async addRess(){
					if(!this.$u.test.chinese(this.userName)){
						this.msg("姓名格式不正确","none")
						return
					}
					if(!this.$u.test.mobile(this.userPhone)){
						this.msg("手机号格式不正确","none")
						return
					}
					if(this.city=="选择地址"){
						this.msg("省市区未选择","none")
						return
					}
					if(this.detail.trim()==""){
						this.msg("详细地址未填写","none")
						return
					}
					let data={
						user_id:uni.getStorageSync("userInfo").id,
						name:this.userName,
						phone:this.userPhone,
						area_id:this.area_id,
						id:this.addressId,
						detail:this.detail
						
					}
					if(this.defaultType==false){
						data.is_default=0
					}
					if(this.defaultType==true){
						data.is_default=1
					}
					console.log("data",data)
					let res=await this.post(addAddress,data)
					this.msg("保存成功")
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},1500)
				}
			}
		}
</script>

<style lang="scss">
	page {
		background: #f6f6f6;
		padding-top: 16upx;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			width: 140upx;
			font-size: 30upx;
			// color: $font-color-dark;
		}

		.input {
			flex: 1;
			font-size: 30upx;
			// color: $font-color-dark;
		}

		.iconfont {
			font-size: 36upx;
			// color: $font-color-light;
		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		// font-size: $font-lg;
		color: #fff;
		background-color: #06923D;
		border-radius: 10upx;
		// box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
